package problems.daily;

/**
 * 1662. 检查两个字符串数组是否相等
 * <p>https://leetcode.cn/problems/check-if-two-string-arrays-are-equivalent/</p>
 *
 * @author habitplus
 * @since 2022/11/1 14:26
 */
public class DT1662 {
    public boolean arrayStringsAreEqual1(String[] word1, String[] word2) {
        int m = word1.length;
        int n = word2.length;

        int i = 0, ip = 0;
        int j = 0, jp = 0;

        while (i < m && j < n) {
            if (ip >= word1[i].length()) {
                if (i == m - 1) break;
                ip = 0;
                ++i;
            }

            if (jp >= word2[j].length()) {
                if (j == n - 1) break;
                jp = 0;
                ++j;
            }

            if (word1[i].charAt(ip) != word2[j].charAt(jp)) return false;

            ++ip;
            ++jp;
        }

        return i == m - 1 && j == n - 1 && word1[i].length() == ip && word2[j].length() == jp;
    }

    public boolean arrayStringsAreEqual2(String[] word1, String[] word2) {

        StringBuilder sb = new StringBuilder();
        for (String w : word1) sb.append(w);

        char[] chs = sb.toString().toCharArray();

        int m = chs.length;
        int j = 0;

        for (String s : word2) {
            for (char c : s.toCharArray()) {
                if (j >= m || chs[j] != c) return false;
                ++j;
            }
        }

        return j == m;
    }

    public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
        StringBuilder sb1 = new StringBuilder();
        for (String w : word1) sb1.append(w);

        StringBuilder sb2 = new StringBuilder();
        for (String w : word2) sb2.append(w);

        return sb1.toString().equals(sb2.toString());
    }



    public static void main(String[] args) {
        System.out.println(new DT1662().arrayStringsAreEqual(new String[]{"ab", "c"}, new String[]{"a", "bc"}));
    }
}
